<!DOCTYPE html>
<html lang="en" th:with="title='租户列表',active='cdkeylist'" xmlns:th="http://www.thymeleaf.org">
<header th:replace="header::headerFragment(${title},${active})"></header>
<body class="fixed-left">
<div id="wrapper">
    <div th:replace="header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">激活码生成记录</h4>
                    </div>
                    <div class="col-md-12">
                        <table class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th width="15%">租户账号</th>
                                <th width="10%">租户端用户数量</th>
                                <th>业务使用次数</th>
                                <th width="10%">资产使用次数</th>
                                <th width="10%">租户使用天数</th>
                                <th width="20%">生成时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <th:block th:each="post : ${list.records}">
                                <tr th:cid="${post.id}">
                                    <td>
                                        <th:block th:text="${post.username}"/>
                                    </td>
                                    <td>
                                        <th:block th:text="${post.userCount}"/>
                                    </td>
                                    <td>
                                        <th:block th:text="${post.businessCount}"/>
                                    </td>
                                    <td>
                                        <th:block th:text="${post.assetsCount}"/>
                                    </td>
                                    <td>
                                        <th:block th:text="${post.daysUsed}"/>
                                    </td>
                                    <td>
                                        <th:block th:text="${#dates.format(post.createTime, 'yyyy-MM-dd HH:mm:ss')}"/>
                                    </td>
                                    <td>
                                        <button class="btn btn-primary btn-xs"
                                                onclick="showLicense(this.getAttribute('license'),this.getAttribute('name'))"
                                                th:license="${post.licenseCode}" th:name="${post.username}">查看激活码
                                        </button>
                                    </td>
                                </tr>
                            </th:block>

                            </tbody>
                        </table>
                        <div th:replace="comm/macros :: pageAdminNav(${list})"></div>
                    </div>
                </div>
                <div th:replace="footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="footer :: footer"></div>
<script type="text/javascript">

    function showLicense(licenseCode, name) {
        //自定页
        layer.open({
            type: 1,
            closeBtn: 0, //不显示关闭按钮
            title: "查看激活码",
            anim: 2,
            area: ['800px', '440px'], //宽高
            shadeClose: true, //开启遮罩关闭
            content: '<form>'
                + '<div class="form-group ">'
                + '<div class="col-sm-12">'
                + '<textarea rows="15" cols="15" readonly="" id="showLicenseCode" class="form-control">' + licenseCode + '</textarea></div>'
                + '</div>'
                + '<div class="form-group ">'
                + '<div class="col-sm-12" style="text-align: center;margin-top: 15px;">'
                + '<button id="copy" type="button" class="btn btn-info">复制</button>'
                + ' <button id="download" onclick="exportRaw(\'' + name + '\')" type="button" class="btn btn-inverse">下载</button>'
                + '</div>'
                + ' </div>'
                + ' </form>'
        });
    }

    let clipboard = new Clipboard('#copy', {
        text: function () {
            return $("#showLicenseCode").text();
        }
    });
    clipboard.on('success', function (e) {
        layer.msg('复制成功');
    });
    clipboard.on('error', function (e) {
        console.log(e);
    });

    function fakeClick(obj) {
        let ev = document.createEvent("MouseEvents");
        ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        obj.dispatchEvent(ev);
    }

    function exportRaw(name) {
        name = name + '激活码.txt';
        let data = $("#showLicenseCode").text();
        let urlObject = window.URL || window.webkitURL || window;
        let export_blob = new Blob([data]);
        let save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
        save_link.href = urlObject.createObjectURL(export_blob);
        save_link.download = name;
        fakeClick(save_link);
    }
</script>
</body>
</html>